<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="负责人" prop="villageName">
        <el-input
          v-model="queryParams.villageName"
          placeholder="请输入网格负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职务" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入职务"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报时间" prop="fillTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.fillTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择上报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basicdata:subgroup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basicdata:subgroup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basicdata:subgroup:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['basicdata:subgroup:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-card>
      <el-scrollbar style="height:100%; width:100%;">
        <el-row style="height:100%; width:2080px;">
          <el-table v-loading="loading" :data="subgroupList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="主键" align="center" prop="id" />
            <el-table-column label="负责人" align="center" prop="villageName" />
            <el-table-column label="联系电话" align="center" prop="phone" />
            <el-table-column label="网格人数" align="center" prop="areaNum" />
            <el-table-column label="党员人数" align="center" prop="partyNum" />
            <el-table-column label="常住人口" align="center" prop="oftenNum" />
            <el-table-column label="流动人口" align="center" prop="flowNum" />
            <el-table-column label="企业数量" align="center" prop="companyNum" />
            <el-table-column label="低保人口" align="center" prop="guardNum" />
            <el-table-column label="残疾人口" align="center" prop="deformityNum" />
            <el-table-column label="出租数" align="center" prop="rentNum" />
            <el-table-column label="房间数" align="center" prop="roomNum" />
            <el-table-column label="实际面积" align="center" prop="actualArea" />
            <el-table-column label="计划面积" align="center" prop="planArea" />
            <el-table-column label="围垦面积" align="center" prop="reclamation" />
            <el-table-column label="上报时间" align="center" prop="fillTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.fillTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['basicdata:subgroup:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['basicdata:subgroup:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-scrollbar>
    </el-card>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社区数据信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="村民名称" prop="groupId">
              <el-select v-model="form.villageId" placeholder="请选择村民" filterable clearable>
                <el-option
                  v-for="dict in villagerOptions"
                  :key="dict.id"
                  :label="dict.villageName"
                  :value="parseInt(dict.id)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="position">
              <el-input v-model="form.position" placeholder="请输入职务" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="网格人数" prop="areaNum">
              <el-input v-model="form.areaNum" placeholder="请输入网格人数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="党员人数" prop="partyNum">
              <el-input v-model="form.partyNum" placeholder="请输入党员人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人员名单" prop="members">
              <el-input v-model="form.members" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="常住人口" prop="oftenNum">
              <el-input v-model="form.oftenNum" placeholder="请输入常住人口" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流动人口" prop="flowNum">
              <el-input v-model="form.flowNum" placeholder="请输入流动人口" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业数量" prop="companyNum">
              <el-input v-model="form.companyNum" placeholder="请输入企业数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="低保人口" prop="guardNum">
              <el-input v-model="form.guardNum" placeholder="请输入低保人口" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="残疾人口" prop="deformityNum">
              <el-input v-model="form.deformityNum" placeholder="请输入残疾人口" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出租数" prop="rentNum">
              <el-input v-model="form.rentNum" placeholder="请输入出租数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="房间数" prop="roomNum">
              <el-input v-model="form.roomNum" placeholder="请输入房间数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际面积" prop="actualArea">
              <el-input v-model="form.actualArea" placeholder="请输入实际面积" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划面积" prop="planArea">
              <el-input v-model="form.planArea" placeholder="请输入计划面积" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="围垦面积" prop="reclamation">
              <el-input v-model="form.reclamation" placeholder="请输入围垦面积" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="上报时间" prop="fillTime">
          <el-date-picker clearable size="small"
            v-model="form.fillTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上报时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSubgroup, getSubgroup, delSubgroup, addSubgroup, updateSubgroup, exportSubgroup } from "@/api/basicdata/subgroup";
import { villagerList } from "@/api/basicdata/villager";

export default {
  name: "Subgroup",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社区数据信息表格数据
      subgroupList: [],
      //用户信息列表
      villagerOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        villageName: null,
        areaPerson: null,
        position: null,
        phone: null,
        areaNum: null,
        partyNum: null,
        members: null,
        oftenNum: null,
        flowNum: null,
        companyNum: null,
        guardNum: null,
        deformityNum: null,
        rentNum: null,
        roomNum: null,
        actualArea: null,
        planArea: null,
        reclamation: null,
        fillTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.communityId = this.$route.params && this.$route.params.communityId;
    this.getvillagerList();
    this.getList();
  },
  methods: {
    //获取村民信息列表
    getvillagerList(){
        villagerList().then(response => {
            this.villagerOptions = response.data;
        });
    },
    /** 查询社区数据信息列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams.communityId);
      listSubgroup(this.queryParams).then(response => {
        this.subgroupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        communityId: null,
        areaPerson: null,
        position: null,
        phone: null,
        areaNum: null,
        partyNum: null,
        members: null,
        oftenNum: null,
        flowNum: null,
        companyNum: null,
        guardNum: null,
        deformityNum: null,
        rentNum: null,
        roomNum: null,
        actualArea: null,
        planArea: null,
        reclamation: null,
        createTime: null,
        fillTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加社区数据信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSubgroup(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改社区数据信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSubgroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.communityId = this.communityId;
            addSubgroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除社区数据信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSubgroup(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有社区数据信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSubgroup(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
